// pages/userInfo/userInfo.js
import Message from "../../miniprogram_npm/tdesign-miniprogram/message/index"
import { addRecoveryMember , addMedicalRecord , getIllnessDictionary} from "../../services/_utils/api"
import { GETSTORAGE , SETSTORAGE} from "../../services/storage/storage"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeValues: [0],
    nameValue: '', //姓名
    sexValue: '点击选择性别', //性别
    heightValue:'',//身高
    weightValue:'',//体重
    weightError:false,//体重正则
    heightError:false,//身高正则
    nameError: false, //姓名正则
    sexError: false, //性别正则
    leftLeg: false, //左腿
    rightLeg: false, //右腿
    treatDoctorValue:'',//主治医生
    hospitalValue:'',//治疗医院
    bedNumberValue:'',//住院床号
    //  picker
    citys: [{
        label: '男',
        value: '男'
      },
      {
        label: '女',
        value: '女'
      },
    ],
    autosize: {
      maxHeight: 120,
      minHeight: 20,
    },
    dateVisible: false,
    date: '1980-01-01', // 出生日期
    dateText: '请选择出生日期',
    // 指定选择区间起始值
    start: '1900-01-01 00:00:00',//出生日期最早时间
    end: '2024',//出生日期最晚时间
    operation:new Date().getTime(), //手术时间
    operationVisible: false,
    operationText: '请选择手术时间', 
    // cardiopathyVisible:false,
    cardiopathyValue:'',
    cardiopathyText:'无', //心脏病
    cerebrovascularText:'无', //脑血管
    hyperthrombosisText:'无', //高血栓
    cardiopathyList:[],
    cerebrovascularList:[],
    hyperthrombosisList:[],
    activeFlag:false,
  },
  handleSwitch(){
    this.setData({
      activeFlag:!this.data.activeFlag
    })
  },
 // 左腿切换
 leftLegChange(e){
  const flag = !this.data.leftLeg
  this.setData({
    leftLeg: flag,
    rightLeg:this.data.rightLeg
  })
},
// 右腿切换
rightLegChange(e){
  const flag = !this.data.rightLeg
  this.setData({
    rightLeg: flag,
    leftLeg:this.data.leftLeg
  })
},
  getIllnessDictionary(){
    getIllnessDictionary().then(res => {
      console.log(res,"病历史");
     res.data.data.allDisease.forEach(item => {
       if(item.medicalHistory == '心脏病史'){
         let obj = {
           value:item.illness,
           label:item.illness
         }
         this.data.cardiopathyList.push(obj)
         this.setData({
          cardiopathyList:this.data.cardiopathyList
         })
       }else if(item.medicalHistory == '脑血管病史'){
        let obj = {
          value:item.illness,
          label:item.illness
        }
        this.data.cerebrovascularList.push(obj)
        this.setData({
          cerebrovascularList:this.data.cerebrovascularList
        })
       }else{
        let obj = {
          value:item.illness,
          label:item.illness
        }
        this.data.hyperthrombosisList.push(obj)
        this.setData({
          hyperthrombosisList:this.data.hyperthrombosisList
        })
       }
     })
    })
  },
  // 完成
  submit(){
    const that = this.data
    const userid = GETSTORAGE('userId')
    let userInfo = {
    userId:userid,
    familyMemberId:"",     //可以赋空
    nickName:that.nameValue,   //昵称
    birth:that.date,       //出生年月日 
    sex:that.sexValue[0],       //性别
    height:that.heightValue,    //身高
    weight:that.weightValue  //体重
    }
    let userCase = {
      userId:userid,
      recoveryMemberInfoId:"",
      leftLeg:that.leftLeg,      //手术左腿   
      rightLeg:that.rightLeg,    //手术右腿
      treatDoctor:that.treatDoctorValue,    //主治医生       
      operativeTime:that.operation,    //手术时间    
      hospital:that.hospitalValue,      //治疗医院     
      heartMedicalHistory: [that.cardiopathyText],     //心脏病
      cerebrovascularMedicalHistory: [that.cerebrovascularText] , //脑血管
      thrombosisMedicalHistory:[that.hyperthrombosisText],//高血栓
      bedNumber:that.bedNumberValue //住院床号
    }
    if(that.nameValue == '' ||that.sexValue == '' || that.date == '' || that.heightValue == '' || that.weightValue == ''){
      Message.error({
        context: this,
        offset: [20, 32],
        duration: 5000,
        content: '输入框不能为空',
      });
      return
    }else{
      console.log(userCase,"病例信息");
      addRecoveryMember(userInfo).then(res => {
        console.log(res);
        if(res.data.code == 200){
          userCase.recoveryMemberInfoId = res.data.data.recoveryMemberInfoId
          SETSTORAGE('recoveryMemberInfoId',res.data.data.recoveryMemberInfoId)
          addMedicalRecord(userCase).then(res => {
            console.log(res,"病历创建");
            SETSTORAGE('medicalRecordId',res.data.data.medicalRecordId)
            if(res.data.code == 200){
               wx.navigateTo({
                 url: '/pages/bindDoctor/bindDoctor',
               })
            }
          })
        }
      })
    }
  },
  // 心脏病确认
  oncardiopathyChange(e){
    const { key } = e.currentTarget.dataset;
    const { value } = e.detail;
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Value`]: value,
      [`${key}Text`]: value.join(' '),
    });
  },
  // 心脏病取消
  oncardiopathyCancel(e){
    const { key } = e.currentTarget.dataset;
    this.setData({
      [`${key}Visible`]: false
    });
  },
  // 心脏病选择器显示
  onCardiopathyPicker(e){
    const { key } = e.currentTarget.dataset;
    this.setData({
      [`${key}Visible`]: true
    })
  },
  // 病状
  onIllnessChange(e){
    const {value} = e.detail
    this.setData({
      illnessValue : value
    })
  },
  // 住院床号
  onBedNumberInput(e){
    const {value} = e.detail
    this.setData({
      bedNumberValue : value
    })
  },
  // 治疗医院
  onHospitalInput(e){
    const {value} = e.detail
    this.setData({
      hospitalValue : value
    })
  },
  // 主治医生
  onTreatDoctorInput(e){
    const {value} = e.detail
    this.setData({
      treatDoctorValue : value
    })
  },
  // 手术腿部
  onChange(e){
    // console.log(e);
    const {value} = e.detail
    if(value.length == 0){
      this.setData({
        leftLeg:false,
        rightLeg:false
      })
    }else if(value.length == 1 && value[0] == 'left'){
      this.setData({
        leftLeg:true,
        rightLeg:false
      })
    }else if(value.length == 1 && value[0] == 'right'){
      this.setData({
        leftLeg:false,
        rightLeg:true
      })
    }else{
      this.setData({
        leftLeg:true,
        rightLeg:true
      })
    }

  },
  // 出生年月日弹出框显示
  showPicker(e) {
    const { mode } = e.currentTarget.dataset;
    this.setData({
      mode,
      [`${mode}Visible`]: true,
    });
  },
  // 出生年月日取消
  hidePicker() {
    const { mode } = this.data;
    this.setData({
      [`${mode}Visible`]: false,
    });
  },
  // 出生年月日确定
  onConfirm(e) {
    const { value } = e.detail;
    const { mode } = this.data;
    this.setData({
      [mode]: value,
      [`${mode}Text`]: value,
    });
    this.hidePicker();
  },
  // 体重输入
  onWeightInput(e){
    this.inputFunction(e,'weight')
  },
  // 身高输入
  onHeightInput(e){
    this.inputFunction(e,'height')
  },
  // 输入函数
  inputFunction(e,val){
    const {
      value
    } = e.detail
    if (value == '') {
      this.setData({
        [`${val}Error`]: true,
        [`${val}Value`]: value
      })
    } else {
      this.setData({
        [`${val}Error`]: false,
        [`${val}Value`]: value
      })
    }
  },
  // 姓名输入
  onNameInput(e){
    this.inputFunction(e,'name')
  },
  handleChange(e){
    this.setData({
      activeValues: e.detail.value,
    });
  },
  // 性别弹出框显示
  onWithoutTitlePicker(){
    this.setData({
      city2Visible: true,
      city2Title: ''
    });
  },
  // 性别确定
  onPickerChange(e) {
    console.log(e);
    const {
      key
    } = e.currentTarget.dataset;
    const {
      value
    } = e.detail;
    this.setData({
      [`${key}Visible`]: false,
      sexValue: value,
      [`${key}Text`]: value.join(' '),
    });
  },
  // 性别取消
  onPickerCancel(e) {
    const {
      key
    } = e.currentTarget.dataset;
    this.setData({
      [`${key}Visible`]: false,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getIllnessDictionary()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})